package com.gs.synedocket.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.TextView;

import com.gs.synedocket.R;
import com.gs.synedocket.activity.base.BaseActivity;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.control.DocketLineView;
import com.gs.synedocket.control.DocketView;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.db.dto.DocketSiteDto;
import com.gs.synedocket.db.dto.ShortDto;
import com.gs.synedocket.dialog.LoadingDialogUtils;
import com.gs.synedocket.dialog.ToastDialogUtils;
import com.gs.synedocket.presenter.DocketPresenter;
import com.gs.synedocket.util.DateUtil;
import com.gs.synedocket.util.PermissionUtil;
import com.gs.synedocket.util.ToastUtil;
import com.gs.synedocket.view.IDocketView;

import java.util.List;

public class DocketActivity extends BaseActivity<IDocketView, DocketPresenter> implements IDocketView {

    private DocketView viewDocketNo, viewContactPerson, viewTelNo, viewVehicleNo,
            viewPlant, viewSlump, viewLoadQty, viewCumulativeQty, viewOrderQty, viewDate,
            viewCementBrand, viewMaxAgg, viewAdmixtures, viewBuyerCode, viewSiteCode,
            viewSpecialTeam, viewCharge1, viewCharge2, viewLastTel;
    private DocketLineView viewBuyerName, viewSiteLoc, viewConcreteMix, viewBatchTime;
    private String phone, location;
    private TextView tvSite, tvTruck, tvDocket;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_docket;
    }

    @Override
    protected void initView() {
        tvTruck = fd(R.id.tv_truck);
        tvDocket = fd(R.id.tv_docket);
        tvSite = fd(R.id.tv_site);
        viewDocketNo = fd(R.id.view_docket_no);
        viewBuyerName = fd(R.id.view_buyer_name);
        viewSiteLoc = fd(R.id.view_site_loc);
        viewContactPerson = fd(R.id.view_contact_person);
        viewTelNo = fd(R.id.view_tel_no);
        viewVehicleNo = fd(R.id.view_vehicle_no);
        viewPlant = fd(R.id.view_plant);
        viewConcreteMix = fd(R.id.view_concrete_mix);
        viewSlump = fd(R.id.view_slump);
        viewLoadQty = fd(R.id.view_load_qty);
        viewCumulativeQty = fd(R.id.view_cumulative_qty);
        viewOrderQty = fd(R.id.view_order_qty);
        viewDate = fd(R.id.view_date);
        viewBatchTime = fd(R.id.view_batch_time);
        viewCementBrand = fd(R.id.view_cement_brand);
        viewMaxAgg = fd(R.id.view_max_agg);
        viewAdmixtures = fd(R.id.view_admixtures);
        viewBuyerCode = fd(R.id.view_buyer_code);
        viewSiteCode = fd(R.id.view_site_code);
        viewSpecialTeam = fd(R.id.view_special_term);
        viewCharge1 = fd(R.id.view_charge1);
        viewCharge2 = fd(R.id.view_charge2);
        viewLastTel = fd(R.id.view_last_tel);
        viewSiteLoc.setOnClickListener(v -> {
            if (TextUtils.isEmpty(location)) {
                ToastUtil.showToast(R.string.no_gps);
                return;
            }
            Intent intent = new Intent(DocketActivity.this, LocationActivity.class);
            intent.putExtra(AllConstants.LOCATION, location);
            startActivity(intent);
        });
        viewTelNo.setOnClickListener(v -> {
            if (TextUtils.isEmpty(phone)) {
                ToastUtil.showToast(R.string.no_tel_no);
                return;
            }
            if (PermissionUtil.addPermission(DocketActivity.this, new String[]{Manifest.permission.CALL_PHONE}, AllConstants.CALL_PHONE)) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                startActivity(intent);
            }
        });
        fd(R.id.iv_back).setOnClickListener(v -> finish());
        String docketNo = getIntent().getStringExtra(AllConstants.DOCKET);
        mPresenter.getScanPaper(docketNo);
    }

    @Override
    protected DocketPresenter createPresenter() {
        return new DocketPresenter();
    }

    @Override
    public void onGetDataSuccess(ApiResultData<DocketDto> apiResult) {
        if (apiResult.getStatus() != 2000 || apiResult.getData() == null) {
            ToastDialogUtils.getInstance().showInfoMessage(this, apiResult.getMessage());
            return;
        }
        DocketDto data = apiResult.getData();
        viewContactPerson.setContent(data.getContactPerson() == null ? "" : data.getContactPerson());
        phone = data.getContactTel() == null ? "" : data.getContactTel();
        viewTelNo.setContent(phone);
        String docketNo = data.getDocketNo() == null ? "" : data.getDocketNo();
        viewDocketNo.setContent(docketNo);
        tvDocket.setText(docketNo);
        tvTruck.setText(data.getTruckNo() == null ? "" : data.getTruckNo());
        ShortDto customer = data.getCustomer();
        if (customer != null) {
            viewBuyerName.setContent(customer.getName() == null ? "" : customer.getName());
            viewBuyerCode.setContent(customer.getCode() == null ? "" : customer.getCode());
        } else {
            viewBuyerName.setContent("");
            viewBuyerCode.setContent("");
        }
        DocketSiteDto site = data.getSite();
        if (site != null) {
            String siteName = site.getName();
            String extraAddress = data.getExtraAddress();
            String name = (siteName == null ? "" : siteName) + (extraAddress == null ? "" : " " + extraAddress);
            viewSiteLoc.setContent(name);
            tvSite.setText(name);
            viewSiteCode.setContent(site.getCode() == null ? "" : site.getCode());
            location = site.getGps();
        } else {
            viewSiteLoc.setContent("");
            viewSiteCode.setContent("");
        }
        ShortDto product = data.getProduct();
        if (product != null) {
            viewConcreteMix.setContent(product.getName() == null ? "" : product.getName());
        } else {
            viewConcreteMix.setContent("");
        }
        ShortDto cement = data.getCement();
        if (cement != null) {
            viewCementBrand.setContent(cement.getName() == null ? "" : cement.getName());
        } else {
            viewCementBrand.setContent("");
        }
        List<ShortDto> admixtures = data.getAdmixtures();
        if (admixtures != null && !admixtures.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (ShortDto bean : admixtures) {
                sb.append(",").append(bean.getName() == null ? "" : bean.getName());
            }
            viewAdmixtures.setContent(sb.substring(1));
        } else {
            viewAdmixtures.setContent("");
        }
        ShortDto plant = data.getPlant();
        if (plant != null) {
            viewPlant.setContent(plant.getName() == null ? "" : plant.getName());
        } else {
            viewPlant.setContent("");
        }
        viewMaxAgg.setContent(data.getNominalMaxAgg() == null ? "" : data.getNominalMaxAgg() + "MM");
        viewSlump.setContent(data.getSlump() == null ? "" : data.getSlump() + "");
        viewVehicleNo.setContent(data.getTruckNo() == null ? "" : data.getTruckNo());
        viewLoadQty.setContent(data.getLoadQty() == null ? "" : String.valueOf(data.getLoadQty()));
        viewCumulativeQty.setContent(data.getCumQty() == null ? "" : String.valueOf(data.getCumQty()));
        viewOrderQty.setContent(data.getOrderQty() == null ? "" : String.valueOf(data.getOrderQty()));
        viewDate.setContent(DateUtil.DateTimeToStr(data.getDocketAt()));
        viewBatchTime.setContent(DateUtil.DateTimeToStr(data.getBatchTime(), "HH:mm"));
        viewSpecialTeam.setContent(data.getSpecialTerm() == null ? "" : data.getSpecialTerm());
        viewCharge1.setContent(data.getAdditionalCharge1() == null ? "" : data.getAdditionalCharge1());
        viewCharge2.setContent(data.getAdditionalCharge2() == null ? "" : data.getAdditionalCharge2());
        viewLastTel.setContent(data.getLastDriverTel() == null ? "" : data.getLastDriverTel());
    }

    @Override
    public void onGetDataFail(Exception e) {
        ToastDialogUtils.getInstance().showInfoMessage(this, R.string.network_error);
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(this);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

}